package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xymarket.adapter.GoodsAdapter;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.httpUtil.GoodsService;
import com.example.xymarket.R;
import com.example.xymarket.po.Goods;
import com.example.xymarket.po.Merchant;

import java.util.List;

public class MerchantActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant);
        initMerchantMessage();
        MyHandler myHandler=new MyHandler(getApplicationContext(),handler);
        GoodsService goodsService=new GoodsService(myHandler);
        goodsService.getGoodsByMerchant(1);
        StatusBar.setStatusBarColor(this,StatusBar.yellow);
    }

    public void initMerchantMessage()
    {
        if(Merchant.merchant!=null)
        {
            TextView name=(TextView)findViewById(R.id.order_name);
            name.setText(Merchant.merchant.getName());
            TextView dis=(TextView)findViewById(R.id.merchant_dis);
            dis.setText(Merchant.merchant.getDescription());
            TextView distance=(TextView)findViewById(R.id.merchant_distance);
            distance.setText(Merchant.merchant.getDistance());
            TextView phone=(TextView)findViewById(R.id.order_price);
            phone.setText("店主联系方式"+Merchant.merchant.getPhone());
            ImageView imageView=(ImageView) findViewById(R.id.order_avatar);
            imageView.setImageDrawable(Merchant.merchant.getDrawable());
        }
    }
    public void updateListView(List<Goods> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.goods_listView);
        try {
            GoodsAdapter adapter = new GoodsAdapter(list,this,R.layout.item_home,handler);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }
    }

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1) {
                updateListView((List<Goods>) msg.obj);
            }
        }
    };

}